import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {PhoneVerification} from "../../modals/basic.modal";
import {AlertComponent} from "../../unit/alert";

@Component({
    selector: 'app-verification-phone',
    templateUrl: './verification-phone.page.html',
    styleUrls: ['../login/login.page.scss', './verification-phone.page.scss'],
})
export class VerificationPhonePage implements OnInit {
    pageTitle: string;
    formData: PhoneVerification;
    currentType: string;

    constructor(private route: Router,
                public alertComponent: AlertComponent,
                private activateInfo: ActivatedRoute,) {
    }

    ngOnInit() {
        this.formInit();
        this.activateInfo.queryParamMap.subscribe((res: any) => {
            this.initPage(res.params);
        });
    }

    initPage(params) {
        this.currentType = params?.type;
        this.pageTitle = params?.type === '1' ? '验证码登录' : params?.type === '2' ? '忘记密码' : '';
    }

    formInit() {
        this.formData = {
            phone: '',
            code: ''
        }
    }

    submitData() {
        if (this.currentType === '1') {
            this.loginIn();
        } else if (this.currentType === '2') {
            this.verificationPhone();
        }
    }

    verificationPhone() {
        // 验证成功后，跳转到登录页 参数：当前手机绑定的帐号
        this.goToPage('reset-password', '', {type: 1, code: '5599'});
    }

    loginIn() {
        this.goToPage('tabs');
    }

    goToPage(url, title = '', item = {}) {
        this.route.navigate([`/${url}`], {
            queryParams:
                {title: title, ...item}
        }).then();
    }

}
